<?php

namespace App\Http\Controllers\Api;

use App\Models\Config;
use App\Models\Goods;
use App\Models\IntegralDetail;
use App\Models\Order;
use App\Models\OrderInfo;
use App\Models\ShopCar;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{
    public function lists(Request $request)
    {
        $where=[
            ['user_id',$request->get('user')->user_id],
        ];
        if ($request->has('status')){
            array_push($where,['order_status',$request->get('status')]);
        }
        $orderes = Order::where($where)
            ->select(['order_id','sum_price','order_status','created_at','freight','use_integral'])
            ->where('order_status','<>',1)
            ->where('order_status','<>',7)
            ->orderBy('created_at','desc')
            ->get();
        foreach ($orderes as $order){
            $my_infos = $order->my_info()->get();
            foreach ($my_infos as $my_info){
                $my_info->cover = $my_info->my_goods->my_picture()->where('type',1)->first()->my_pic_path;
                $order->freight=number_format($order->freight,2);
            }
            $order->goods =$my_infos;
            if($order->order_status==2){
                $order->order_status="未付款";
            }
            if($order->order_status==3){
                $order->order_status="已付款";
            }
            if($order->order_status==4){
                $order->order_status="已发货";
            }
            if($order->order_status==5){
                $order->order_status="已完成";
            }
            if($order->order_status==6){
                $order->order_status="已取消";
            }
            if($order->order_status==7){
                $order->order_status="已删除";
            }
        }
        return ['status_code'=>1,'data'=>$orderes];
    }


    public function create(Request $request)
    {
        $order = new Order();
        $order->save();
        $sum_price=0;
        $goodsinfo=[];
        foreach ($request->get('goods') as $value){
            $goods = Goods:: find($value['goods_id']);
            $info = [
                'goods_id'=> $value['goods_id'],
                'price'   => $goods->price,
                'num'     => $value['num'],
                'order_id'=> $order->order_id,
            ];
            OrderInfo::create($info);
            $goods->cover= $goods->my_picture()->where('type',1)->first()->my_pic_path;
            $goods->num = $value['num'];
            $goodsinfo[]=$goods;
            $sum_price+=$value['num']*$goods->price;
        }
        $order->sum_price = $sum_price;
        $order->order_status    = 1;
        $order->order_sn  = 'O'.time();
        $order->user_id = $request->get('user')->user_id;
        $order->save();
        $order->orderinfo = $goodsinfo;
        $order->sum_goods = count($request->get('goods'));
        $order->person_integral = ($request->get('user')->user_integral)/($rule = Config::where('name','JFDH')->first()->value);
        $yf = Config::where('name','YF')->first()->value;
        $order->yf = $yf;
        return ['status_code'=>1,'data'=>$order];
    }

    public function confirm(Request $request)
    {
        $order = Order::find($request->get('order_id'));

        if($request->get('is_confirm')==1){
            foreach ($order->my_info as $value){
                $goods = Goods::find($value->goods_id);
                $goods->stock-=$value->num;
                if($goods->stock<0){
                    return ['status_code'=>0,'message'=>"库存不足"];
                }
                $goods->save();
            }
            $order->receive_type=$request->get('reveive_type');
            if($order->receive_type==2){
                $yf = Config::where('name','YF')->first();
                $order->receive_id=$request->get('receive_id');
                $order->freight =$yf->value;
            }
            $order->note =$request->get('note',"无");
            if($request->is_integral==1){
                //积分规则
                //todo 积分规则
                $rule = Config::where('name','JFDH')->first();
                $user = $request->get('user');
                $order->use_integral = round(($user->user_integral)/($rule->value),2);
                $rule = Config::where('name','JFSC')->first();
                $order->back_integral = $order->sum_price*$rule->value;
                $intergral_detail = new IntegralDetail();
                $intergral_detail->type = 1;
                $intergral_detail->user_id =$user->user_id;
                $intergral_detail->value =$user->user_integral;
                $user ->user_integral = 0;
                $intergral_detail->save();
                $user ->save();
            }else{
                $order->use_integral=0;
            }
            $order->order_status = 2;
            $order->save();
            if($request->get('shopcar_id')!=""){
                foreach ($request->get('shopcar_id') as $value){
                    ShopCar::destroy($value);
                }
            }
        }else{
            OrderInfo::where('order_id',$order->order_id)->delete();
            $order->delete();
        }
        return ['status_code'=>1,'message'=>"OK"];
    }

    public function detail(Request $request)
    {

        if($order = Order::where('order_id',$request->get('order_id'))
            ->first()){
            $order->freight=number_format($order->freight,2);
            $order->total = $order->sum_price+$order->freight-$order->use_integral;
            $goods = $order->my_info()->get(['price','num','goods_id']);
            foreach ($goods as $v){
                $v->cover = $v->my_goods->my_picture()->where('type',1)->first()->my_pic_path;
            }
            $order->goods=$goods;
            if($order->receive_type==1){
                $order->receive_addr="到店自提";
            }elseif($order->receive_type==2){
                 $receive_addr = $order->my_receive()->first();
                 $receive_addr->province_name;
                 $receive_addr->city_name;
                 $receive_addr->area_name;
                 $order->receive_addr = $receive_addr->province_name->province.
                     $receive_addr->city_name->city.
                     $receive_addr->area_name->area.
                     $receive_addr->addr;
                 $order->receive_phone = $receive_addr->phone;
                 $order->receive_name = $receive_addr->name;
            }
            if($order->order_status==2){
                $order->order_status="未付款";
            }
            if($order->order_status==3){
                $order->order_status="已付款";
            }
            if($order->order_status==4){
                $order->order_status="已发货";
            }
            if($order->order_status==5){
                $order->order_status="已完成";
            }
            if($order->order_status==6){
                $order->order_status="已取消";
            }
            if($order->order_status==7){
                $order->order_status="已删除";
            }
            return ['status_code'=>1,'data'=>$order];
        }else{
            return ['status_code'=>0,'message'=>"没有这个订单"];
        }
    }

    public function update(Request $request)
    {
        if($order = Order::find($request->get('order_id'))){
            $order->order_status=$request->get('status');
            $order->save();
            return ['status_code'=>1,'message'=>"更新成功"];

        }else{
            return ['status_code'=>0,'message'=>"没有这个订单"];
        }
    }
}
